package com.ideaction;

import com.ideaction.bimodule.server.CommonBiServer;
import com.ideaction.game.bus.RedissionBusService;
import com.ideaction.game.utils.SensitiveWords;
import com.ideaction.game.xmldata.ISample;
import com.ideaction.game.xmldata.SampleContainer;
import com.ideaction.service.ActivityService;
import com.ideaction.test.TimerTest;
import com.ideaction.utils.GlobalSwitchUtil;
import com.log.service.GameLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * @author ZGame spring 启动完成后,加载程序其他组件 主线程
 */
@Slf4j
@Component
@EnableScheduling
public class CommandRunner implements CommandLineRunner {
    
    @Value("${localization.config}")
    private String localizationConfig;// 本地化
    
    @Value("${logging.config}")
    private String loggingConfig;// 本地log
    
    @Value("${sensitive.config}")
    private String sensitiveConfig;// 敏感词
    
    @Value("${xmldata.config}")
    private String xmldataConfig;// xml
    
    @Autowired
    private GameLogService gameLogService;// 数据库日志
    
    @Autowired
    private RedissionBusService redissionBusService;// 总线
    
    @Autowired
    private CommonBiServer commonBiServer;//
    
    @Autowired
    private TimerTest timerTest;// 定时器timer demo
    
    @Autowired
    private ActivityService activityService;
    
    @Autowired
    private SampleContainer<ISample> sampleContainer;

    @Autowired
    private GlobalSwitchUtil globalSwitchUtil;

    @Override
    public void run(String... args) throws Exception {
        configureTasks();
        sampleContainer.load();
        // 加载xml数据
        //sampleManager.init(xmldataConfig);
        log.info("加载xml配置数据完成");
        // 加载敏感词
        SensitiveWords.initAC(sensitiveConfig);
        log.info("加载敏感词完成");
        // 启动mongodb日志线程
        gameLogService.start();
        log.info("mongodb数据库日志启动完成");
        // 启动消息总线bus
        redissionBusService.start();
        commonBiServer.start();
        log.info("消息总线启动完成");
        activityService.updateActivityData();
        log.info("更新活动");
        globalSwitchUtil.upGlobalSwitch();
        log.info("加载全局功能开关完成");
    }

    private void configureTasks() {
        Executors.newSingleThreadScheduledExecutor().scheduleWithFixedDelay(() -> {
            timerTest.timer();
            globalSwitchUtil.upGlobalSwitch();
            //log.info("加载全局功能开关完成");
        }, 1, 5, TimeUnit.SECONDS);


    }
    
    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
}
